package com.clh.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.clh.common.PageInfo;
import com.clh.dto.Result;
import com.clh.entity.Complaint;
import com.clh.entity.PayType;
import com.clh.service.PayTypeService;
import com.clh.service.impl.PayTypeServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Servlet implementation class PayTypeServlet
 */
@WebServlet("/PayTypeServlet")
public class PayTypeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private PayTypeService payTypeService = new PayTypeServiceImpl();

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 接受请求参数区分不同请求
		String op = request.getParameter("op");
		if ("find".equals(op)) {
			doGetPayTypeList(request, response);
		} else if ("page".equals(op)) {// 执行查询
			this.doTypePageQuery(request, response);
		} else if ("delete".equals(op)) {
			this.dodelete(request, response);
		} else if ("update".equals(op)) {
			this.doUpdate(request, response);
		} else if ("add".equals(op)) {
			this.doAddPayType(request, response);
			System.out.println("dasd");
		} else if ("findById".equals(op)) {
			this.dofindByNo(request, response);
		}
	}

	/**
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void dofindByNo(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 统一的响应对象格式
		Result result = new Result();
		// 接受请求参数
		String TypeId = request.getParameter("TypeId");
		if (TypeId == null || "".equals(TypeId.trim())) {
			// System.out.println("空的");
			result.fail(0, "参数错误");
		} else {
			// 调用业务层
			PayType payType = payTypeService.getPayTypeById(Integer.valueOf(TypeId));
			if (payType != null) {
				result.ok(payType);
			} else {
				result.fail(0, "没有相关数据");
			}
		}
		// 响应
		response.setContentType("application/json;charset=utf-8");
		// 利用第三方库jackson转换
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(result);
		// 创建响应并将数据返回给浏览器
		response.getWriter().write(json);

	}

	/**
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void doAddPayType(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 统一的响应对象格式
		Result result = new Result();
		// 接受请求参数
		String TypeName = request.getParameter("TypeName");
		System.out.println(TypeName);
		// 构建对象
		PayType payType = new PayType();
		payType.setTypeName(TypeName);
		boolean f = payTypeService.addPayType(payType);
		if (f) {
			result.ok(f);
		} else {
			result.fail(0, "项目信息添加失败");
		}

		// 调用业务层
		// ...
		// 响应
		response.setContentType("application/json;charset=utf-8");
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(result);
		// 创建响应并将数据返回给浏览器
		response.getWriter().write(json);
	}

	/**
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void doUpdate(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 统一的响应对象格式
		Result result = new Result();
		// 接受请求参数
		String TypeId = request.getParameter("typeId");
		String TypeName = request.getParameter("typeName");
		PayType payType = new PayType();
		System.out.println(TypeId);
		payType.setTypeId(Integer.valueOf(TypeId));
		payType.setTypeName(TypeName);
		boolean f = payTypeService.modifyPayType(payType);
		if (f) {
			result.ok(f);
		} else {
			result.fail(0, "项目信息修改失败");
		}

		// 调用业务层
		// ...
		// 响应
		response.setContentType("application/json;charset=utf-8");
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(result);
		// 创建响应并将数据返回给浏览器
		response.getWriter().write(json);
	}

	/**
	 * @param request
	 * @param response
	 */
	private void dodelete(HttpServletRequest request, HttpServletResponse response) {

		// 统一的响应对象格式
		Result result = new Result();
		try {
			String typeId = request.getParameter("TypeId");
			if (typeId == null || "".equals(typeId)) {
				result.fail(400, "参数错误");
			} else {
				int n = payTypeService.deleteById(Integer.valueOf(typeId));
				if (n > 0) {
					result.ok(true).setMessage("删除成功");
				} else {
					result.fail(0, "删除失败，可能原因是它不存在，或者被关联引用！");
				}
			}
//							// 设置相应的内容类型
			response.setContentType("application/json;charset=utf-8");
			// 利用第三方库jackson转换
			ObjectMapper objectMapper = new ObjectMapper();
			String json = objectMapper.writeValueAsString(result);
			// 创建响应并将数据返回给浏览器
			response.getWriter().write(json);

		} catch (Exception e) {
			result.fail(500, "服务器出错了");
		}

	}

	/**
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	private void doTypePageQuery(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 统一的响应对象格式
		Result result = new Result();

		// 接受请求参数
		String pageNum = request.getParameter("pageNum");
		String pageSize = request.getParameter("pageSize");
		String TypeName = request.getParameter("typeName");
		// 对请求参数进行校验
				if ((pageNum == null || "".equals(pageNum) || Integer.parseInt(pageNum) < 0)
						|| (pageSize == null || "".equals(pageSize) || Integer.parseInt(pageSize) < 0)) {
					result.fail(0, "参数错误");
				} else {
					// 调用业务层
					PageInfo<PayType> pageInfo = payTypeService.getPayTypeByPage(TypeName,Integer.parseInt(pageNum), Integer.parseInt(pageSize));
					if (!pageInfo.getList().isEmpty()) {
						result.ok(pageInfo);
					} else {
						result.fail(0, "没有相关数据");
					}
				}
				// 响应
				response.setContentType("application/json;charset=utf-8");
				ObjectMapper objectMapper = new ObjectMapper();
				String json = objectMapper.writeValueAsString(result);
				// 创建响应并将数据返回给浏览器
				response.getWriter().write(json);
	}

	/**
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void doGetPayTypeList(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 统一响应格式
		Result<List<PayType>> result = new Result<List<PayType>>();
		// 调用业务层
		List<PayType> list = payTypeService.getPayTypeList();
		if (!list.isEmpty()) {
			result.ok(list);
		} else {
			result.fail(0, "无相关数据");
		}
		// 响应
		response.setContentType("application/json;charset=utf-8");
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(result);
		response.getWriter().write(json);

	}

}
